package com.study.rabbitmq.advance;

import com.rabbitmq.client.*;

import java.io.IOException;

public class PullRecv {
	private final static String QUEUE_NAME = "hello";

	public static void main(String[] argv) throws Exception {
		ConnectionFactory factory = new ConnectionFactory();
		factory.setHost("192.168.20.68");
		factory.setPort(5672);
		factory.setUsername("leke");
		factory.setPassword("leke@@@");
		Connection connection = factory.newConnection();
		Channel channel = connection.createChannel();

		channel.basicQos(10);
		//channel.queueDeclare(QUEUE_NAME, false, false, false, null);

		System.out.println(" [*] Waiting for messages. To exit press CTRL+C");

		while (true) {
			Thread.sleep(1000);
			//手动ack
			GetResponse response = channel.basicGet(QUEUE_NAME, false);
			if (response != null) {
				System.out.println(" Received: " + new String(response.getBody()));
				channel.basicAck(response.getEnvelope().getDeliveryTag(), false);
			}

//自动ack
//			GetResponse response =  channel.basicGet(QUEUE_NAME, true);
//			if(response!=null){
//				System.out.println(" Received: "+new String(response.getBody()));
//			}
		}


	}
}
